﻿Imports System.Data.SqlClient

Public Class CabinaDAO



    Public Function listar() As Collection
        Dim lista As New Collection

        Try
            Dim cn As New SqlConnection(ConexionBD.obtenerCadenaConexion())
            cn.Open()

            Dim sql = "SELECT *  FROM PEDIDO"
            Dim cmd As New SqlCommand(sql, cn)

            Dim dr As SqlDataReader
            dr = cmd.ExecuteReader()
            While dr.Read
                Dim look As New Cabina
                look.IdCabina = dr(0).ToString
                look.Nombre_encargado = dr(1).ToString
                look.Direccion = dr(2).ToString

                lista.Add(look)
            End While

            dr.Close()
            cn.Close()

        Catch ex As SqlException
            Throw New DAOExcepcion(ex.ToString)
        End Try

        Return lista
    End Function

    Public Function buscarPorNombre(ByVal nombre As String) As Collection
        Dim lista As New Collection

        Try
            Dim cn As New SqlConnection(ConexionBD.obtenerCadenaConexion())
            cn.Open()

            Dim sql = "SELECT * FROM cabina WHERE UPPER(Nombre_encargado) LIKE @VALOR1"
            Dim cmd As New SqlCommand(sql, cn)
            cmd.Parameters.AddWithValue("@VALOR1", "%" + nombre.ToUpper + "%")

            Dim dr As SqlDataReader
            dr = cmd.ExecuteReader()

            While dr.Read
                Dim modelo As New Cabina
                modelo.IdCabina = dr(0).ToString
                modelo.Nombre_encargado = dr(1).ToString
                modelo.Direccion = dr(2).ToString

                lista.Add(modelo)
            End While

            dr.Close()
            cn.Close()

        Catch ex As SqlException
            Throw New DAOExcepcion(ex.ToString)
        End Try

        Return lista
    End Function



    Public Function insertar(ByVal demo As Cabina) As Cabina
        Try
            Dim cn As New SqlConnection(ConexionBD.obtenerCadenaConexion())
            cn.Open()

            Dim sql = "INSERT INTO CABINA (Nombre_encargado, Direccion) VALUES (@VALOR1, @VALOR2)"

            Dim ins As New SqlCommand(sql, cn)

            ins.Parameters.AddWithValue("@VALOR1", demo.Nombre_encargado)
            ins.Parameters.AddWithValue("@VALOR2", demo.Direccion)


            Dim afectados = ins.ExecuteNonQuery

            If afectados <> 1 Then
                Throw New DAOExcepcion("CategoriaDAO: No insertó")
            End If

            ' LEER EL ULTIMO ID
            sql = "SELECT @@IDENTITY"
            ins = New SqlCommand(sql, cn)
            Dim dr As SqlDataReader
            dr = ins.ExecuteReader()
            If dr.Read Then

                demo.IdCabina = CType(dr(0), Integer)
            End If

            dr.Close()
            cn.Close()

        Catch ex As Exception
            Throw New DAOExcepcion(ex.ToString)
        End Try

        Return demo


    End Function




End Class
